package core.object
{
	import core.manager.AnimationManager;
	import core.manager.SpriteSheetManager;
	
	import events.GameAnimationEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import core.IDispose;
	
	/**
	 * 有些地方还没处理好,功能差不多能使用
	 * TODO 需要优化添加一些MovieClip的功能,尽量不去支持帧标签的功能
	 */ 
	public class GameAnimation extends Bitmap implements IDispose
	{
		public var loop:Boolean = true;
		
		public var func:Function;// 每次执行一个循环后需要的操作
		
		public function GameAnimation(id:int = -1,isRunning:Boolean = true,pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			super(null, pixelSnapping, smoothing);
			this.id = id
			this._isRunning = isRunning;
			if(this.id != -1){
				this.initialize();
			}
		}
		
		public function initialize():void {
			if(this._isRunning){
				AnimationManager.instance.addItem(this);
			}else {
				this.redraw();
			}
		}
		
		private var _id:int;
		
		public function set id(value:int):void {
			this._id = value;
			this._sheet = SpriteSheetManager.instance.getSheet(value);
		}
		
		public function get id():int {
			return this._id;
		}
		
		protected var _sheet:SpriteSheet;
		
		private var _frame:int = -1;
		
		public function redraw():void {
			if(!this._sheet){
				return;
			}
			this._frame++;
			this.bitmapData = _sheet.list[this._frame];
			if(this._frame >= _sheet.maxFrame){
				if(!loop){
					this._isRunning = false;
				}
				if(this.func != null){
					this.func();
				}
				this._frame = -1;
			}
			this.smoothing = true;
			this.dispatchEvent(new GameAnimationEvent(GameAnimationEvent.REDRAW_EVENT));
		}
		
		public function get sheet():SpriteSheet {
			return this._sheet;
		}
		
		private var isPlaying:Boolean;
		
		public function play():void {
			this.isPlaying = true;
			
		}
		
		private var _isRunning:Boolean = true;
		
		public function stop():void {
			this._isRunning = false;
			this.isPlaying = false;
		}
		
		public function update():void {
			this.redraw();
		}
		
		public function dispose():void {
			AnimationManager.instance.removeItem(this);
			if(this.parent){
				this.parent.removeChild(this);
			}
		}
		
		public function clone():GameAnimation {
			return null;
		}
		
		/**
		 * 
		 * @param frame 帧数,要int型,暂时不支持帧标签的方式
		 * 
		 */		
		public function gotoAndStop(frame:int):void {
			this._frame = frame;
			this.bitmapData = _sheet.list[this._frame];
		}
	}
}